package com.shop.shop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.shop.shop.entity.Address;
import com.shop.shop.service.AddressService;
import com.shop.shop.utile.JWTUtils;
import com.shop.shop.utile.MapUtile;
import com.shop.shop.utile.Q;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 地址表 前端控制器
 * </p>
 *
 * @author 小🌤
 * @since 2021-04-22
 */
@RestController
@RequestMapping("/shop/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 重新全部
     *
     * @param mapUtile
     * @return
     */
    @GetMapping("list")
    public Q list(MapUtile mapUtile) {
        Map<String, Object> map = addressService.findByPage(mapUtile);
        return Q.ok().data(map);
    }

    /**
     * 保存
     *
     * @param address
     * @return
     */
    @PostMapping("save")
    public Q save(@RequestBody Address address,HttpServletRequest request) {
        Map<String, Object> map = JWTUtils.verify(request.getHeader("token"));
        Integer id= Integer.parseInt(map.get("id").toString());
        address.setUrId(id);
        addressService.save(address);
        return Q.ok();
    }

    /**
     * id 查询
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    public Q info(@PathVariable("id") Integer id) {
        Address address = addressService.getById(id);
        return Q.ok().data("address", address);
    }

    /**
     * id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Q delete(@PathVariable("id") Integer id) {
        boolean b = addressService.removeById(id);
        return b ? Q.ok() : Q.error();
    }


    /**
     * id修改
     *
     * @param address
     * @return
     */
    @PostMapping("updateDf")
    public Q update(@RequestBody Address address, HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String, Object> map = JWTUtils.verify(token);
        String id = (String) map.get("id");
        boolean b = addressService.updateIdUser(address, id);
        return b ? Q.ok() : Q.error();
    }

    /**
     * 修改
     *
     * @param address
     * @return
     */
    @PostMapping("update")
    public Q update(@RequestBody Address address) {
        boolean b = addressService.updateById(address);
        return b ? Q.ok() : Q.error();
    }

    @GetMapping("findById")
    public Q findById(HttpServletRequest request){
        Map<String, Object> map = JWTUtils.verify(request.getHeader("token"));
        int id = Integer.parseInt(map.get("id").toString());
        Address address= addressService.findByIDDF(id);
      return Q.ok().data("data",address);
    }
}

